<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .red-1 {
      background-color: red;
    }
    .green {
      background-color: green;
    }
  </style>
</head>
<body>

<div id="app">
  <h1>{{ msg }}</h1>
  <h2>{{ reversedMsg }}</h2>
  <hr>
  <h2>{{ reversedMsgMethod() }}</h2>
  <hr>
  <h1>计算属性时间戳: {{ now }}</h1>
  <hr>
  <h1>fullName: {{ fullName }}</h1>
</div>

<script src="js/vue.js"></script>
<script>
  const app = new Vue({
    data: {
      msg: "hello vue 计算属性 vs 侦听器!",
      // fullName: "john 张三",
      firstName: "john",
      lastName: "张三"
    },
    computed: {
      fullName: {
        get: function () {
          return this.firstName + " " + this.lastName
        },
        set: function (newValue) {
          let names = newValue.split(' ')
          this.firstName = names[0]
          this.lastName = names[names.length - 1]
        }
      },
      // fullName() {
        // return this.firstName + " " + this.lastName
      // },
      // 重构data, 缓存
      // 计算属性是基于它们的响应式依赖进行缓存的
      reversedMsg() {
        return this.msg.split("").reverse().join()
      },
      now() {
        return Date.now()
      }
    },
    // 侦听属性
    watch: {
      /*
      firstName() {
        this.fullName = this.firstName + " " + this.lastName
      },
      lastName() {
        this.fullName = this.firstName + " " + this.lastName
      },*/
      msg(after, before) {
        console.log(after)
      }
    },
    methods: {
      /*
      now() {
        return Date.now()
      }*/
      reversedMsgMethod() {
        return this.msg.split("").reverse().join()
      },
    }
  }).$mount("#app")
</script>
</body>
</html>
